package com.itheima.homework.test3;

import java.util.HashSet;

/*
现有若干图书信息（包含名称title、作者author、定价price）需要存储到set集合中，保证集合中无重复元素，并遍历查看。
可以认为所有信息都相同的图书为重复数据。
 */
public class Demo3 {
    public static void main(String[] args) {
        HashSet<Book> hs = new HashSet<>();
        Book b1 = new Book("论如何找女朋友","张三",1);
        Book b2 = new Book("论如何哄女朋友","李四",2);
        Book b3 = new Book("论平底锅的重要性","王五",5);
        Book b4 = new Book("论如何找女朋友","张三",1);
        hs.add(b1);
        hs.add(b2);
        hs.add(b3);
        hs.add(b4);
        for (Book h : hs) {
            System.out.println(h);
        }

    }
}
class Book{
    private String title;
    private String author;
    private int price;

    public Book() {
    }

    public Book(String title, String author, int price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        if (price != book.price) return false;
        if (title != null ? !title.equals(book.title) : book.title != null) return false;
        return author != null ? author.equals(book.author) : book.author == null;
    }

    @Override
    public int hashCode() {
        int result = title != null ? title.hashCode() : 0;
        result = 31 * result + (author != null ? author.hashCode() : 0);
        result = 31 * result + price;
        return result;
    }
}